package com.sparkseries.controller;

import com.sparkseries.common.util.domain.Result;
import com.sparkseries.domain.vo.SysCompanyVO;
import com.sparkseries.domain.vo.SysDepartmentVO;
import com.sparkseries.domain.vo.SysUserVO;
import com.sparkseries.dto.SysCompanyDTO;
import com.sparkseries.service.SysCompanyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 企业管理控制器
 */
@RestController
@RequestMapping("company")
@Tag(name = "企业管理")
public class SysCompanyController {
    @Resource
    private SysCompanyService sysCompanyService;

    /**
     * 创建企业
     *
     * @param sysCompanyDTO 企业传输类
     * @return 默认响应类
     */
    @PostMapping("createCompany")
    @Operation(summary = "创建企业")
    //@PreAuthorize("")
    public Result<?> createCompany(@RequestBody SysCompanyDTO sysCompanyDTO) {
        return sysCompanyService.createCompany(sysCompanyDTO);
    }

    /**
     * 删除企业
     *
     * @param companyId 企业id
     * @return 默认响应类
     */
    @DeleteMapping("deleteCompany/{companyId}")
    @Operation(summary = "删除企业")
    //@PreAuthorize("")
    public Result<?> deleteCompany(@PathVariable("companyId") Long companyId) {
        return sysCompanyService.deleteCompany(companyId);
    }


    /**
     * 修改企业
     *
     * @param sysCompanyDTO 企业传输类
     * @return 默认响应类
     */
    @PutMapping("updateCompany")
    @Operation(summary = "修改企业")
    //@PreAuthorize("")
    public Result<?> updateCompany(@RequestBody SysCompanyDTO sysCompanyDTO) {
        return sysCompanyService.updateCompany(sysCompanyDTO);
    }

    /**
     * 修改企业状态
     *
     * @param companyId 企业id
     * @param status    状态     0: 失效 1: 激活
     * @return 默认响应类
     */
    @PutMapping("updateStatus/{companyId}")
    @Operation(summary = "修改企业状态")
    @Parameter(name = "status", required = true)
    //@PreAuthorize("")
    public Result<?> updateStatus(@PathVariable("companyId") Long companyId, @RequestParam("status") Integer status) {
        return sysCompanyService.updateStatus(companyId, status);
    }

    /**
     * 获取企业信息
     *
     * @param companyId 企业id
     * @return 企业响应类
     */
    @GetMapping("getCompany/{companyId}")
    @Operation(summary = "获取企业信息")
    //@PreAuthorize("")
    public Result<SysCompanyVO> getCompany(@PathVariable("companyId") Long companyId) {
        return sysCompanyService.getCompanyById(companyId);
    }

    /**
     * 企业列表
     * 包含筛选和模糊查询
     * 可筛选条件为企业类型，企业证件类型，企业状态
     * TODO 模糊查询名字和备注
     *
     * @param sysCompanyDTO 企业传输类
     * @param detail        企业名称模糊查找关键词，可为空
     * @return 企业响应类的集合
     */
    @GetMapping("listCompany")
    @Operation(summary = "企业列表")
    //@PreAuthorize("")
    public Result<List<SysCompanyVO>> listCompany(@RequestBody(required = false) SysCompanyDTO sysCompanyDTO, @RequestParam(value = "detail", required = false, defaultValue = "") String detail) {
        return sysCompanyService.listCompany(sysCompanyDTO, detail);
    }

    /**
     * 获取企业管理员信息
     *
     * @param companyId 企业ID
     * @return 用户响应类
     */
    @GetMapping("getCompanyAdminInfo/{companyId}")
    @Operation(summary = "获取企业管理员信息")
    //@PreAuthorize("")
    public Result<SysUserVO> getCompanyAdmin(@PathVariable("companyId")Long companyId){
        return sysCompanyService.getCompanyAdmin(companyId);
    };

    /**
     * 企业添加部门
     *
     * @param departmentIds 部门id
     * @param companyId     企业id
     * @return 默认响应类
     */
    @PostMapping("insertDepartments/{companyId}")
    @Operation(summary = "企业添加部门")
    //@PreAuthorize("")
    public Result<?> insertDepartments(@RequestBody Long[] departmentIds, @PathVariable("companyId") Long companyId) {
        return sysCompanyService.insertDepartments(departmentIds, companyId);
    }

    /**
     * 企业删除部门
     *
     * @param departmentIds 部门id
     * @param companyId     企业id
     * @return 默认响应类
     */
    @DeleteMapping("deleteDepartments/{companyId}")
    @Operation(summary = "企业删除部门")
    //@PreAuthorize("")
    public Result<?> deleteDepartments(@RequestBody Long[] departmentIds, @PathVariable("companyId") Long companyId) {
        return sysCompanyService.deleteDepartments(departmentIds, companyId);
    }

    /**
     * 查找某企业下的所有部门
     *
     * @param companyId 用于查找的企业id
     * @return 部门响应类的集合
     */
    @GetMapping("getDepartmentsByCompanyId/{companyId}")
    @Operation(summary = "查找某企业下的所有部门")
    //@PreAuthorize("")
    public Result<List<SysDepartmentVO>> getDepartmentsByCompanyId(@PathVariable("companyId") Long companyId) {
        return sysCompanyService.listDepartmentByCompanyId(companyId);
    }
}
